var appWxShareDirective = angular.module("appWxShareDirective",[]);
appWxShareDirective.directive("wxShare",function(appWxShareService,$location){
    return {
        template: "<div><div/>",
        restrice: "EACM",
        replace: true,
        scope:{
            ngRes:"=ngRes",
        },
        controller:function($scope,$element,$attrs){
            
            $scope.$watch("ngRes",function(newValue,oldValue){
                $scope.ngRes = newValue;
                if($scope.ngRes!=undefined)
                appWxShareService.share($scope.ngRes);
            });
            
            
        }
    }
})

appWxShareDirective.service("appWxShareService",['$http','$location',function($http,$location){
    var cururl  = location.href;
    cururl = cururl.replace(location.hash,'');
    //console.log(cururl);
    this.share = function(ngRes){
        
        var  transFn = function(data) {
            return $.param(data);
        };
        var postCfg = {
            headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
            transformRequest: transFn
        };
        var sc = document.querySelector("#wxsc");
        var flag = !!sc;
        if(!flag){
            sc = document.createElement('SCRIPT');
                document.body.appendChild(sc);          
        }
        sc.src = "http://res.wx.qq.com/open/js/jweixin-1.0.0.js";    
        var count = 5;

        function share(ngRes){ 
            $http.post("/sign/index.php/Home/Sign/index",{url:cururl,t:new Date().getMilliseconds()},postCfg)
                .success(function(data, status, headers, config) {
                    //console.log(angular.toJson(data));
                    wx.config({
                        debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                        appId: data.appId, // 必填，公众号的唯一标识
                        timestamp: data.timestamp, // 必填，生成签名的时间戳
                        nonceStr: data.nonceStr, // 必填，生成签名的随机串
                        signature: data.signature,// 必填，签名，见附录1
                        jsApiList: [
                            'onMenuShareAppMessage',
                            'onMenuShareTimeline'
                        ] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
                    });
                    //var isDetail = location.hash.indexOf('home/detail')!=-1;
                    //var title = "我已报名考拉星课堂【只要会说话，即评不可怕】,这是第1次上课，坚持";
                    // var pic = "http://"+location.hostname+"/enroll/static/img/logo.png";
                    //console.log(ngRes);
                    console.log(ngRes);
                    var sharetitle = "我已报名考拉星课堂【"+ngRes.title+"】,这是第"+ngRes.times+"次上课，坚持!";
                    var sharedesc =  "一起来听考拉星讲堂吧！."+ngRes.title+"——"+ngRes.t_name+" "+ngRes.school_name+" "+ngRes.academy;//isDetail?title:"见过|扒一扒老师的故事、小爱好与口头禅，或者你还知道更多......";
                    var shareimg = "http://"+location.hostname+"/enroll/static/img/logo1.png";
                    var shareurl = location.href;
                    var shareData = {
                        title: sharetitle,
                        desc: sharedesc,
                        link: shareurl,
                        imgUrl: shareimg,
                        success: function (res) {
                        },
                        cancel: function (res) { 
                        }
                    };               
                    wx.ready(function () {
                      //好友
                      wx.onMenuShareAppMessage(shareData);
                      wx.onMenuShareTimeline(shareData);
                    }); 
                    wx.error(function(res){
                        if(count-- >0 ){
                            var data = {'res':res,'shareData':shareData,'count':count};
                            $http.post("/sign/index.php/Home/Sign/logError",{data:angular.toJson(data)},postCfg);
                            
                            share(ngRes);
                        }else{
                            var data = {'res':res,'shareData':shareData,'count':count,'note':'放弃了！','absUrl':$location.absUrl()};
                            
                            $http.post("/sign/index.php/Home/Sign/logError",{data:angular.toJson(data)},postCfg);
    //                      $location.path($location..absUrl());
                        }
                    });
                }).error(function(data, status, headers, config) {
        //      alert('啊呀，分享配置失效啦，容我再试一次！');
                });               
            }
            
            sc.onload = function(){
                share(ngRes);
            }

        
    };

    
}])
